<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/5
 * Time: 14:41
 */

namespace app\shopping\controller;

use app\shopping\model\Commalltool;
use app\shopping\model\Commdetailstool;

class Classifycomm {
    private $Commalltool;
    private $Commdetailstool;

    public function __construct()
    {
        $this->Commalltool = new Commalltool();
        $this->Commdetailstool = new Commdetailstool();
    }

    function index(){
        $comm_sort = getParameter("comm_sort");
        $action = getParameter("action");

        if ($action == "all") {
            $this->Commalltool->getAllComm($comm_sort);
        } else if ($action == "search") {
            $search = $_GET["search"];
            $this->getSearch($comm_sort, $search);
        } else if ($action == "detail") {
            $this->getDetails($comm_sort);
        }else{
            echo echoJson(0, "参数错误,-1!");
        }
    }

    /**
     *  得到搜索商品
     * @param str $uid 账号
     * @return boolean
     */
    function getSearch($comm_sort, $search) {
        if (!empty($search)) {
            $this->Commalltool->getSearchComm($comm_sort, $search);
        } else {
            echo echoJson(0, "搜索条件为空!");
        }
    }

    /**
     *  得到商品详情
     * @param str $comm_sort 商品类型
     */
    function getDetails($comm_sort) {
        $comm_id = getParameter("comm_id");
        if ($comm_id) {
            $jsonArr = $this->Commdetailstool->getCommDetails($comm_sort, $comm_id, true);
            if (is_array($jsonArr) && count($jsonArr) > 0) {
                echo echoJson(1, $jsonArr);
            } else {
                echo echoJson(-1, "暂无详情!");
            }
        } else {
            echo echoJson(0, "参数错误,-3!");
        }
    }
}